package makabakaquestionnaire.zutsoftware.framework.cloud;

import cn.hutool.core.util.IdUtil;
import makabakaquestionnaire.zutsoftware.constant.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
public class OssFileController {
    @Autowired
    OssUtil ossUtil;
    @PostMapping("/project/file/upload")
    public Result uploadFile(@RequestParam("file")MultipartFile file) throws IOException {
        String OriginalFileName=file.getOriginalFilename();   //获得文件的原始名称
        String name=OriginalFileName.substring(0,OriginalFileName.lastIndexOf("."));
        String lujing="wj/";
        String path=new StringBuffer(lujing+IdUtil.simpleUUID())
                .append(name)
                .append(".")
                .append(OriginalFileName.substring(OriginalFileName.lastIndexOf(".")+1)).toString();
        String url =ossUtil.upload(file.getInputStream(),path);
        return Result.success(url);
    }
    /**
     *  查找所有文件
     *  返回列表
     */
    @GetMapping("/project/file/getFileList")
    public  Result getFileList(){
       List list= ossUtil.getObjectList();
       return Result.success(list);
    }
}
